const executiveCommand = require('../db/index')
const md5 = require('md5')
const jwt = require('jsonwebtoken');
// 生成 Token
function generateToken(payload) {
    const token = jwt.sign(payload, global.secretKey);
    return token;
}
function getToken(data) {
    return new Promise(async (re, rj) => {
        let result = await executiveCommand(`select * from users WHERE username = "${data.username}"`)
        if (result.length !== 0) {
            let { username, password } = result[0]
            if (md5(data.password + global.md5) === password) {
                generateToken({ username, password })
                re(
                    { msg: '登录成功', data: generateToken({ username, password }) }
                )
            }
            rj('账号或密码错误')
        } else {
            rj('账号或密码错误')
        }
    })
}

module.exports = {
    getToken
}